using Godot;
using System.Collections.Generic;

namespace AndroidUnevenRoadGame.Game
{
    public static class CharacterDatabase
    {
        private static readonly Dictionary<string, CharacterDefinition> _characters = new();
        private static bool _isLoaded;

        public static void LoadFromJson(string resourcePath)
        {
            if (_isLoaded)
            {
                return;
            }

            var file = FileAccess.Open(resourcePath, FileAccess.ModeFlags.Read);
            if (file == null)
            {
                GD.PushWarning($"无法打开角色配置：{resourcePath}");
                return;
            }

            var jsonText = file.GetAsText();
            file.Dispose();

            var result = Json.ParseString(jsonText);
            if (result.VariantType != Variant.Type.Array)
            {
                GD.PushWarning("角色配置格式错误：根节点需要为数组");
                return;
            }

            foreach (Variant entry in result.AsGodotArray())
            {
                if (entry.VariantType != Variant.Type.Dictionary)
                {
                    continue;
                }

                var dict = entry.AsGodotDictionary();
                var deck = new List<string>();

                if (dict.TryGetValue("starting_deck", out var deckVar) &&
                    deckVar.VariantType == Variant.Type.Array)
                {
                    var deckArray = deckVar.AsGodotArray();
                    foreach (Variant cardVar in deckArray)
                    {
                        if (cardVar.VariantType == Variant.Type.String)
                        {
                            deck.Add(cardVar.AsString());
                        }
                    }
                }

                var id = dict.TryGetValue("id", out var idVar) && idVar.VariantType == Variant.Type.String
                    ? idVar.AsString()
                    : "unknown";

                var portrait = dict.TryGetValue("portrait", out var portraitVar) && portraitVar.VariantType == Variant.Type.String
                    ? portraitVar.AsString()
                    : $"res://Assets/Textures/Characters/{id}.jpg";

                var definition = new CharacterDefinition(
                    id,
                    dict.TryGetValue("name", out var nameVar) && nameVar.VariantType == Variant.Type.String ? nameVar.AsString() : id,
                    dict.TryGetValue("initial_hp", out var hpVar) && hpVar.VariantType == Variant.Type.Int ? hpVar.AsInt32() : 80,
                    dict.TryGetValue("energy_per_turn", out var energyVar) && energyVar.VariantType == Variant.Type.Int ? energyVar.AsInt32() : 3,
                    deck,
                    portrait);

                _characters[id] = definition;
            }

            _isLoaded = true;
        }

        public static CharacterDefinition Get(string id)
        {
            if (_characters.TryGetValue(id, out var definition))
            {
                return definition;
            }

            GD.PushWarning($"未找到角色：{id}");
            return CharacterDefinition.Default;
        }

        public static IEnumerable<CharacterDefinition> GetAll()
        {
            return _characters.Values;
        }
    }
}

